<?php
namespace Home\Controller;
use Think\Controller;
class TjController extends Controller {
	 public function _initialize(){
	   if(session("username")){
		
			
		}else{
			  $this->redirect("Login/index"); 
		}
		$this->assign('index',6);
   }
    public function index(){
		 $danwei=M('danwei');
		 $fenzu=M('fenzu');
		
		 $where="";
		
		 $count =$fenzu->where($where)->count();
		 $Page= new \Think\Page($count,10);
		 
		 $show= $Page->show();// 分页显示输出
		 
		 $list=$fenzu->where($where)->order('id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
		   foreach ($list as $key => &$value) {
			   $sumcount = $danwei->where("zuid=".$value["id"])->count();
			   $zhucecount = $danwei->where("zuid=".$value["id"]." and jointime is not null")->count();
			   $nozhucecount = $danwei->where("zuid=".$value["id"]." and jointime is  null")->count();
			   $onlinecount = $danwei->where("zuid=".$value["id"]." and online=1")->count();
			   $noonlinecount = $danwei->where("zuid=".$value["id"]." and online=0")->count();
			   $value["sumcount"]=$sumcount;
			   $value["zhucecount"]=$zhucecount;
			   $value["nozhucecount"]=$nozhucecount;
			   $value["onlinecount"]=$onlinecount;
			   $value["noonlinecount"]=$noonlinecount;
		   }
		 $this->assign('alist',$list);
		 $this->assign('page',$show);
	
		layout('Layout/layout');
		$this->display();
      
    }
	
	public function daochu(){
		$danwei =D("danwei");
		$fenzu =D("fenzu");
		$zuid=$_GET["zuid"];
		$online=$_GET["online"];
		$where=" zuid=".$zuid." and online=".$online;
		$list=$danwei->where($where)->select();
		$data = array();
		 foreach ($list as $key => &$value) {
			  $zuid =$value["zuid"];       
			  $zuinfo =$fenzu->where("id=".$zuid)->find();
			  $zuname=$zuinfo["zuname"];
			  $danweiname= $value["name"];
			  $jointime=substr($value["jointime"],0,10);
			  if($online=="1"){
				  $outtime="";
			  }else{
				 $outtime=substr($value["outtime"],0,10);
			  }
			
			  $jointime = date('Y-m-d H:i:s',$jointime);
			  $outtime = date('Y-m-d H:i:s',$outtime);
			  array_push($data,array($danweiname,$zuname,$jointime,$outtime));
		  }
		
        $header='单位名称,所属分组,进入时间,离线时间';
		create_csv($data,$header,"tv.csv");
		
	}
	
	
	
	


}